import { message, TablePaginationConfig } from "antd";
import dayjs from "dayjs";
import { mapValues } from "lodash-es";

import {
  H_M_S,
  Y_M_D,
  Y_M_D_H_M,
  Y_M_D_H_M_S,
  Y_M_D_T_H_M_S_S,
  Y_M_D_T_H_M_S_Z,
} from "~/modules/shared/models/times";
import { ChartType } from "~/modules/workspace/models";

const success = (msg: string) => {
  message.success(msg, 3);
};
const error = (msg: string) => {
  message.error(msg, 3);
};
const warning = (msg: string) => {
  message.warning(msg, 3);
};

const formatDate = (date: any) => {
  if (date === "" || date === null) return "";
  return dayjs(date).format(Y_M_D);
};

const formatDateTime = (date: any) => {
  if (date === "" || date === null) return "";
  return dayjs(date).format(Y_M_D_H_M_S);
};
const formatDateTimeSS = (date: any) => {
  if (date === "" || date === null) return "";
  return dayjs(date).format(Y_M_D_T_H_M_S_S);
};

const formatTime = (date: any) => {
  if (date === "" || date === null) return "";
  return dayjs(date).format(H_M_S);
};

const formatDateToMin = (date: any) => {
  if (date === "" || date === null) return "";
  return dayjs(date).format(Y_M_D_H_M);
};
const formatDateTZ = (date: any) => {
  if (date === "" || date === null) return "";
  return dayjs(date).format(Y_M_D_T_H_M_S_Z);
};

const getDayjsDate = (date: any, predicate: Function): string => {
  if (date === "" || date === null) return "";
  return predicate(dayjs(date));
};

const pageSize = 10;
const PAGE_SIZE_MODAL_NUMBER = 5;

const getPaginationOption = (data: any, params?: TablePaginationConfig) => {
  return {
    position: [data?.position ? data?.position : "bottomLeft"],
    current: data.current,
    pageSize: data.pageSize,
    total: data.total,
    showTotal,
    showQuickJumper: true,
    showSizeChanger: true,
    pageSizeOptions: ["10", "20", "50", "100"],
    ...params,
  } as TablePaginationConfig;
};
const showTotal = (total: string | number) => {
  return `共${total}条`;
};

const trimSpacing = (data: any) =>
  mapValues(data, (elem: string | number) => {
    if (typeof elem === "string") return elem.trim();
    return elem;
  });

const getchartType = (type: string) => {
  const map = new Map([
    ["折线图", ChartType.LINE],
    ["面积图", ChartType.LINEAREA],
    ["散点图", ChartType.SCATTER],
  ]);
  return map.get(type) || ChartType.LINE;
};

export {
  formatDate,
  formatDateTime,
  pageSize,
  PAGE_SIZE_MODAL_NUMBER,
  message,
  success,
  error,
  warning,
  getPaginationOption,
  showTotal,
  trimSpacing,
  getDayjsDate,
  formatDateToMin,
  getchartType,
  formatDateTZ,
  formatTime,
  formatDateTimeSS,
};
